package net.zjitc.controller;

import com.alipay.api.FileItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.zjitc.Utils.CommonUtils;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.pojo.Evaluate;
import net.zjitc.pojo.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.MvcNamespaceHandler;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/26 15:43
 * @description:
 */
//商品相关
@Controller
public class AdminProduct {
  @Autowired
  private ProductService productService;
  @Autowired
  private CategoryService categoryService;
  //商品显示(后台)
  @RequestMapping("/findAllProduct")
  public ModelAndView findAllProduct(
      @RequestParam(name = "page") Integer page
  ){
    ModelAndView mv = new ModelAndView();
    PageHelper.startPage(page,18);
    List<Product> pageBean = productService.findAllProduct();
    PageInfo pageInfo = new PageInfo(pageBean);
    mv.addObject("p",pageBean);
    System.out.println(pageBean);
    System.out.println(pageInfo);
    mv.addObject("pageInfo",pageInfo);
    mv.setView(new InternalResourceView("shelll68/product/Product.jsp"));
    return mv;
  }

  //商品显示(前端)
  @RequestMapping("/findByCid")
  public ModelAndView findByCid(
      @RequestParam(name = "page") Integer page,
      @RequestParam(name = "cid") Integer cid
  ){
    ModelAndView modelAndView = new ModelAndView();
    List<Category> list=categoryService.findAll();
    System.out.println(list);
    modelAndView.addObject("clist",list);
    //分页查询(根据cid查询一级菜单下的所有产品)
    PageHelper.startPage(page,8);
    List<Product> productList=productService.findPageByCid(cid);
    PageInfo pageInfo = new PageInfo(productList);
    modelAndView.addObject("cid",cid);
    modelAndView.addObject("pageInfo",pageInfo);
    System.out.println(pageInfo);
    //转发到一级菜单列表.jsp
    modelAndView.setView(new InternalResourceView("/user/productList.jsp"));
    return modelAndView;
  }
  //跳转至商品添加
  @RequestMapping("/addUIProduct")
  public ModelAndView addUIProduct(){
        ModelAndView modelAndView =new ModelAndView();
    List<CategorySecond> list=categoryService.findAllSecond();
    modelAndView.addObject("clist",list);
    modelAndView.setView(new InternalResourceView("/shelll68/product/add.jsp"));
    return modelAndView;
  }
  //添加商品
  @RequestMapping("/addProduct")
  public ModelAndView addProduct(
      HttpServletRequest request,
      Product product,
      @RequestParam("file") MultipartFile file
      ) throws IOException {
    ModelAndView mv =new ModelAndView();
    if (!file.isEmpty()){
      String path =request.getServletContext().getRealPath("/products/5");
      String fileName =CommonUtils.uuid()+file.getOriginalFilename();
      File file1path = new File(path,fileName);
      if (!file1path.getParentFile().exists()){
        file1path.getParentFile().mkdirs();
      }
      file.transferTo(new File(path+File.separator+fileName));
      product.setImage("products/5/"+fileName);
      product.setPdate(new Date());
      productService.addProduct(product);
      mv.setView(new InternalResourceView("/findAllProduct.do?page=1"));
      return mv;
    }else {
      mv.setView(new InternalResourceView("/shelll68/product/add.jsp"));
      return mv;
    }
  }
  //跳转至商品修改
  @RequestMapping("/editUIProduct")
  public ModelAndView editUIProduct(@RequestParam(name = "pid") Integer pid){
      ModelAndView mv = new ModelAndView();
      Product product=productService.findByPid(pid);
    mv.addObject("pr",product);
    List<CategorySecond> list=categoryService.findAllSecond();
    mv.addObject("clist",list);
    mv.setView(new InternalResourceView("/shelll68/product/edit.jsp"));
    return mv;
  }
  //修改商品
  @RequestMapping("/editProduct")
  public ModelAndView editProduct(
      HttpServletRequest request,
      Product product,
      @RequestParam("file") MultipartFile file) throws IOException {
    ModelAndView mv =new ModelAndView();
    if (!file.isEmpty()){
      String path =request.getServletContext().getRealPath("/products/5");
      String fileName =CommonUtils.uuid()+file.getOriginalFilename();
      File file1path = new File(path,fileName);
      if (!file1path.getParentFile().exists()){
        file1path.getParentFile().mkdirs();
      }
      file.transferTo(new File(path+File.separator+fileName));
      product.setImage("products/5/"+fileName);
      product.setPdate(new Date());
      productService.updateProduct(product);
      mv.setView(new InternalResourceView("/findAllProduct.do?page=1"));
      return mv;
    }else {
      product.setPdate(new Date());
      productService.updateProduct(product);
      mv.setView(new InternalResourceView("/findAllProduct.do?page=1"));
      return mv;
    }
  }
  //删除商品
  @RequestMapping("/deleteProduct")
  public ModelAndView deleteProduct(@RequestParam(name = "pid") Integer pid){
    ModelAndView mv =new ModelAndView();
    Integer count=productService.deleteProduct(pid);
    mv.setView(new RedirectView("/findAllProduct.do?page=1"));
    return mv;
  }
  //左侧边栏+产品显示
  @RequestMapping("/findByCsid")
  public ModelAndView findByCsid(
        @RequestParam(name = "page") Integer page,
      @RequestParam(name = "csid") Integer csid
  ){
    ModelAndView modelAndView =new ModelAndView();
        //查询到的一级菜单里包含二级菜单列表（左侧边栏）
    List<Category> list=categoryService.findAll();
    System.out.println(list);
    modelAndView.addObject("clist",list);
    //分页查询(根据csid查询二级菜单所属的所有产品)
    PageHelper.startPage(page,8);
    List<Product> productList=productService.findPageByCsid(csid);
    PageInfo pageInfo = new PageInfo(productList);
    System.out.println(pageInfo);
    //将csid放入request域里
    modelAndView.addObject("cid",csid);
    modelAndView.addObject("pageInfo",pageInfo);
    //转发到二级菜单列表.jsp
    modelAndView.setView(new InternalResourceView("user/product_secondList.jsp"));
    return modelAndView;
  }
  //所有评价
  @RequestMapping("/productFindByPid")
  public ModelAndView productFindByPid(
      @RequestParam(name = "pid") Integer pid
  ){
    ModelAndView mv = new ModelAndView();
    //通过传入对pid找到产品
    Product product=productService.findByPid(pid);
    mv.addObject("pro",product);
    //左侧边栏内容传入
    List<Category> list=categoryService.findAll();
    mv.addObject("clist",list);
    //评价
    List<Evaluate> evaluateList=productService.findEvaluate(pid);
    mv.addObject("eva",evaluateList);
    mv.setView(new InternalResourceView("/user/product.jsp"));
    return mv;
  }
}
